# Create your views here.
from django.shortcuts import get_object_or_404, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.template import RequestContext
from django.contrib.auth.models import User
from cartoon_us.clan.models import clan
from django.contrib.auth import authenticate
from cartoon_us.accounts.models import vote
from cartoon_us.gallery.models import ArtWork,WrittenWork,Comic

def blank(request):
    return HttpResponseRedirect('home')

#displays data on the homepage
def default(request):
    data={}

    #if the user is logged in push their user info to the screen
    if request.user.is_authenticated():
        data['logged_in']=True
        data['username']=request.user.username
    else:
        data['logged_in']=False

    #shows the 5 most recently added clans
    c=clan.objects.all()
    if len(c)>5:
        p=[]
        for i in range(5):
            p.append(c[len(c)-i-1])
        data['clans']=p
    else:
        data['clans']=c

    #sorts by rating    
    data['users']=sorted(vote.objects.all(), key=lambda vote: vote.score(), reverse=True)[:3]

    #selects artwork to display on the homepage
    k=ArtWork.objects.all()
    data['ill']=k[len(k)-4:]
    data['topill']=sorted(k, key=lambda ArtWork: ArtWork.vote(), reverse=True)[:3]
    #selects written work to display on the homepage
    tt=WrittenWork.objects.all()
    for t in tt:
        t.abstract=t.abstract[0:200]+' ...'
    data['newfictions']=tt[len(tt)-3:]
    cc=Comic.objects.all()
    for t in cc:
        t.abstract=t.abstract[0:200]+'...'
    data['newcomics']=cc[len(cc)-2:]
    # new coimcs and top comics
    data['topcomics']=sorted(cc, key=lambda Comic: Comic.vote(), reverse=True)[:3]
    #sorts by rating
    tt=sorted(tt, key=lambda WrittenWork: WrittenWork.vote(), reverse=True)[:3]
    data['topfictions']=tt
    return render_to_response('home/home_page.html',data)

